
var setting = {
    view: {
        dblClickExpand: false,
        showLine: true,
        selectedMulti: false,
        expandSpeed: "fast"
    },
    data: {
        simpleData: {
            enable:true,
            idKey: "id",
            pIdKey: "pId",
            rootPId: ""
        }
    },
    callback: {
        beforeClick: function(treeId, treeNode) {
            $('#tb1').form('reset');
            $('#dictionary').datagrid('load',{ pId:treeNode.id, sort: 'sort', order: 'asc'});
        }
    }
};
function initZTree() {
    var zNodes =[];
    $.ajax({
        type: "POST",
        url: ROOT_PATH+'/permission/dictionaryData/treeList.shtml',
        data: {},
        async: false,
        dataType: "json",
        success: function(result){
            zNodes=zNodes.concat(result);
            $('#dictionary').datagrid({
                method: 'post',
                url: ROOT_PATH + '/permission/dictionaryData/list.shtml',
                queryParams: {
                    pId:"0",
                    sort: 'sort',
                    order: 'asc'
                },
                // idField: 'id',
                fitColumns: true,
                pagination: true,
                rownumbers: true,
                singleSelect:true,
                sortOrder: true,
                striped: true,
                fit: true,
                columns: [[
                    {field: 'id', width: 80, checkbox: true},
                    {field: 'code', title: '数据字典标识', width: 100,  sortable: true,hidden:true},
                    {field: 'name', title: '数据字典名称', width: 80,  sortable: true},
                    {
                        field: 'readonly',
                        title: '是否可编辑',
                        width: 60,

                        sortable: true,
                        formatter: function (value, row, index) {
                            if (value == 1) {
                                return '是';
                            } else {
                                return '否';
                            }
                        }
                    },
                    {field: 'remarks', title: '备注', width: 100 }
                ]],
                toolbar: '#toolbar'
            });

        }
    });
    var t = $("#tree");
    $.fn.zTree.init(t, setting, zNodes);
}

$(function(){
    initZTree();
    var zTree = $.fn.zTree.getZTreeObj("tree");
    zTree.selectNode(zTree.getNodeByParam("id", 0));
    //添加按钮事件
    $('#btn_dictionaryData_add').on('click',function () {
        var zTree2 = $.fn.zTree.getZTreeObj("tree");
       var nodes= zTree2.getSelectedNodes();

        WindowManager.openLayer({
            type: 2,
            title: '添加数据字典',
            area: ['600px', '70%'],
            content: ROOT_PATH + '/permission/dictionaryData/add.shtml?pId='+nodes[0].id+'&name='+nodes[0].name,
            callBack:function (data) {
                var zTree = $.fn.zTree.getZTreeObj("tree");
                //zTree.selectNode(zTree.getNodeByParam("id", data));
                zTree.addNodes(zTree.getNodeByParam("id", data.pId),{id:data.id, pId:data.pId,name:data.name});
                $('#dictionary').datagrid('load',{ pId:data.pId, sort: 'sort', order: 'asc'});
            }
        });
    });
    //编辑按钮事件
    $('#btn_dictionaryData_edit').on('click',function () {

        var checkedRows=$('#dictionary').datagrid('getChecked');

        if(checkedRows.length == 0){
            layer.alert("选择需要编辑的行！");
            return
        }else if (checkedRows.length > 1){
            layer.alert("只能选中一行数据！");
            return
        }
        var readonly=checkedRows[0].readonly;
        if(readonly==0){
            layer.alert("不可编辑！");
            return
        }
        var id=checkedRows[0].id;
        WindowManager.openLayer({
            type: 2,
            title: '编辑数据字典',
            area: ['600px', '70%'],
            content: ROOT_PATH + '/permission/dictionaryData/edit.shtml?id='+id,
            callBack:function (data) {
                var zTree = $.fn.zTree.getZTreeObj("tree");
                var node=zTree.getNodeByParam("id", data.id);
                node.name=data.name;
                zTree.updateNode(node);
                $('#dictionary').datagrid('load',{ pId:data.pId, sort: 'sort', order: 'asc'});
            }
        });
    });
    //删除按钮事件
    $('#btn_dictionaryData_delete').on('click',function () {

        var checkedRows=$('#dictionary').datagrid('getChecked');

        if(checkedRows.length == 0){
            layer.alert("选择需要删除的行！");
            return
        }else if (checkedRows.length > 1){
            layer.alert("只能选中一行数据！");
            return
        }
        var readonly=checkedRows[0].readonly;
        if(readonly==0){
            layer.alert("不可删除！");
            return
        }
        var id=checkedRows[0].id;
        layer.confirm('是否确定删除当前选中数据？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.ajax({
                type: "POST",
                url: ROOT_PATH+'/permission/dictionaryData/delete.shtml',
                data: {id:id},
                dataType: "json",
                success: function(result){
                    if (result.success) {
                        $('#dictionary').datagrid('clearSelections').datagrid('reload');
                        var zTree = $.fn.zTree.getZTreeObj("tree");
                        zTree.removeNode(zTree.getNodeByParam("id", id));
                        layer.msg('删除成功', {
                            icon: 1,
                            time: 1000
                        });
                    }
                    else {
                        layer.msg('删除失败', {
                            icon: 2,
                            time: 1000
                        });
                    }
                }
            });
        });

    });

    $("#btn_search").click(function () {
        var name=$("#name").textbox("getValue");
        var zTree2 = $.fn.zTree.getZTreeObj("tree");
        var nodes= zTree2.getSelectedNodes();
        $('#dictionary').datagrid('load',{ pId:nodes[0].id,name:name, sort: 'sort', order: 'asc'});
    });
    $('#btn_clear').on("click", function () {
        $('#tb1').form('reset');
    });
    //input框变化时查询节点
    document.getElementById("keyword").addEventListener("input", test, false);
   /* $("#keyword").blur(function () {
        $('#tb1').form('reset');
        var zTree3 = $.fn.zTree.getZTreeObj("tree");
        var node3= zTree3.getSelectedNodes();
        $('#dictionary').datagrid('load',{ pId:node3[0].id, sort: 'sort', order: 'asc'});
    });*/
    $("#keyword").bind('input propertychange',function () {
            $('#tb1').form('reset');
            var zTree3 = $.fn.zTree.getZTreeObj("tree");
            var node3= zTree3.getSelectedNodes();
            $('#dictionary').datagrid('load',{ pId:node3[0].id, sort: 'sort', order: 'asc'});
    });
});
function test(){
    var treeObj = $.fn.zTree.getZTreeObj("tree");
    var keywords=$("#keyword").val();
    var nodes = treeObj.getNodesByParamFuzzy("name", keywords, null);
    if (nodes.length>0) {
        treeObj.selectNode(nodes[0]);
    }
}

